package bishi.alibaba;

/**
 * @Info:
 * @Author: Tangxz
 * @Date: 2020/09/06 19:05
 */
public class _41 {
    public static void main(String[] args) {
        System.out.println(solution("BAABBBAAABABBAABAAABBBABBBBAABAABAABABAAAAABAAABABBBBBBBABABABBBBBABBBBABABABBBBAABBBBABABBBBBABBAABBAAAABAAABAAABABABABBBAABABABBBBABBABBAAABAAABBAAAABAABAABABBBAAAAABABBBBBBAABBBBAABAAAABBAABBABABABAAABBBABAAABBABBAABAABABAAABBABAABABBABABAABBBAAAAAABBBABBBABBABBBAAABBABABABBAAAABABBAABBBAABABAAABBABBBBBBBAAABBABBBABBAAAABBBABBBABBAAABBBBABBABABAABAAAABBBABBAAAAAAAAAAABAABAABBBAAABBAABBBBAABABAAAABBBAABBBBBBABBBABBBAAAABBBABBBBAABAABBABBAAABABBBAABBBBBBBBBBBABBABBBBAABAABBABBBBBAAAAAABABABBABA"));
    }

    public static int solution(String str) {
        int left = 0;
        int right = 0;
        int num = 0;
        while(right < str.length()){
            while (right<str.length()-1&&str.charAt(right)-str.charAt(right+1)==0){
                right++;
            }
            if (right==str.length()-1){
                break;
            }
            left = right;
            while (right<str.length()-1&&str.charAt(right)-str.charAt(right+1)!=0){
                right+=2;
                num+=2;
            }
            str = str.substring(0,left)+str.substring(right,str.length());
            right = left>0?left-1:left;
        }
        return num;
    }
}
